require.config({
    baseUrl:"./modules",
    paths:{
        jq:"../libs/jquery",
        
        

    }
})
require(["jq"],()=>{
    var otel = document.getElementById("tel");
    var otxt = document.getElementById("txt");
    var otexts = document.getElementById("txets");
    // var osend = document.getElementById("regsiter");
    var ostrengthb = document.querySelector(".strength-b");
    var ostrengthc = document.querySelector(".strength-c");
    var ostrengthd = document.querySelector(".strength-d");
    var telFlag=txtFlag=textsFlag = false;
    otel.onblur = function(){
       console.log(1)
        if(/^1[3-9]\d{9}$/.test(this.value)){
           otel.nextElementSibling.innerHTML = "√";
            telFlag = true
        }else{
            otel.nextElementSibling.innerHTML = "X";
            telFlag = false
        }
    }
    
    
    otxt.onblur = function(){
        var a=b=c=0;
        if(/\d/g.test(this.value)){
            a = 1;
        }
        if(/[a-z]/gi.test(this.value)){
            b = 1;
        }
        if(/[^\da-z]/gi.test(this.value)){
            c = 1;
        }
        switch(a+b+c){
            case 1:ostrengthb.style.backgroundColor="#f48c60";break;
            case 2:ostrengthb.style.backgroundColor="#f48c60";ostrengthc.style.backgroundColor="#f15b17";break;
            case 3:ostrengthb.style.backgroundColor="#f48c60";ostrengthc.style.backgroundColor="#f15b17";ostrengthd.style.backgroundColor="red";break;
        }
    
        txtFlag = true;
    
        if(otexts.value === "") return;
        if(otexts.value === this.value){
            otxt.nextElementSibling.innerHTML = "√";
            textsFlag = true
        }else{
            otxt.nextElementSibling.innerHTML = "X"
            textsFlag = false
        }
    }
    
    otexts.onblur = function(){
        if(this.value === otxt.value){   
            this.nextElementSibling.innerHTML = "√" 
            textsFlag = true
        }else{
            this.nextElementSibling.innerHTML = "X"
            textsFlag = false
        }
    }
    
    
    // 绑定点击事件
    $("#regsiter").click(function(){
        if(telFlag & txtFlag & textsFlag ){
        // console.log(1)
        // 发起ajax
        $.ajax({
            url:"http://localhost:3000/api",
            // ajax发送输入框的数据
            data:{
                type:"register",
                username:$("#tel").val(),
                password:$("#txets").val()
            },
            success:(res)=>{
                console.log(res)
                res = JSON.parse(res);
                if(res.code === 0){
                    alert("你的用户名已被使用，换一个吧");
                    $("#tel").val("");
                    $("#tel").focus();
                }else{
                    if(confirm("注册成功，是否需要跳转到登录？")){
                        location.href = "./loginba.html";
                    }
                }
            }
            
            
        })
        
        
    }
    })

})